Ext.namespace('Taxamatch');
Taxamatch.TestAuthorities = function(config) {
	
	this.store = new Ext.data.JsonStore({
			fields: ['rank', 'match_type', 'genus', 'genus_species', 'temp_authority', 'genus_ed', 'species_ed', 'input']
		,	url: '../api/taxamatch.php'
		,	root: 'data'
		,	idProperty: 'name'		
	});
		
	this.sm =	new Ext.grid.RowSelectionModel({
			singleSelect: true
		,	listeners: {
				rowselect: function(sm, row, rec){
					this.keyName = rec.data.sciname;
				}
			}
	});

	Ext.apply(this, config, {
			width: 750
		,	height: 250
		,	layout: 'fit'
		,	modal: true
		,	resizable: false
		,	defaults: {
					border: false
				,	xtype: 'button'
			}
		,	plain: true
		,	closable: true		
		,	items: [{
					xtype: 'grid'
				,	ref: 'grid1'	
				,	store: this.store
				,	sm: this.sm
				,	loadMask: {msg: 'Loading...'}
				,	scope: this
				,	viewConfig: {
						emptyText: 'No Results Found'
					}
				,	columns: [
							{id:'input', header: 'Input', width: 105, sortable: true, dataIndex: 'input'}
						,	{id:'rank', header: 'Rank', width: 65, sortable: true, dataIndex: 'rank'}
						,	{header: 'Type', width: 75, sortable: true, dataIndex: 'match_type', renderer: this.renderMatchType }
						,	{header: 'Genus', width: 75, sortable: true, dataIndex: 'genus'}
						,	{header: 'Species', width: 130, sortable: true, dataIndex: 'genus_species'}
						,	{header: 'Author', width: 105, sortable: true, dataIndex: 'temp_authority'}
						,	{header: 'Genus ED', width: 70, sortable: true, dataIndex: 'genus_ed'}
						,	{header: 'Species ED', width: 70, sortable: true, dataIndex: 'species_ed'}
					]
			}]
		,	tbar: {
					xtype: 'toolbar'
				,	items: [{
							xtype: 'tbtext'
						,	text: 'Scientific Name: '
					}, '&nbsp;', {
						xtype: 'textfield'
					//,	emptyText: this.txtemptyText
					,	scope: this
					,	ref: '../txtSearch'	
					,	enableKeyEvents: true
					,	listeners: {
								keypress: function(el, e){
									var serachVal= this.txtSearch.getValue();
									if(e.keyCode == 13){
										if(Ext.isEmpty(serachVal)){
											Taxamatch.Notice.msg('Notice', 'Enter search text');
										} else {
											this.searchSci(serachVal);
										}
									}
								}
							,	scope: this
						}
					}, '&nbsp;', {
							text: 'Search'
						,	scope: this
						,	ref: '../btnSearch'
						,	iconCls: 'icon_load'
						,	handler: function(){
								var serachVal = this.txtSearch.getValue();
								if(Ext.isEmpty(serachVal)){
									Taxamatch.Notice.msg('Notice', 'Enter search text');
								} else {
									this.searchSci(serachVal);
								}
							}	
					}]
			}
		,	fbar: {
					xtype: 'toolbar'
				,	items: [{
							text: 'Close'
						,	scope: this
						,	handler: function(){
								this.close();
							}	
					}]
			}
	});
	
	Taxamatch.TestAuthorities.superclass.constructor.call(this, config);
	
};

Ext.extend(Taxamatch.TestAuthorities, Ext.Window, {

	searchSci: function(searchVal){
		this.store.load({
				params: {
						cmd: 'taxamatch'
					,	debug: 0
					,	layout: 'flat'
					,	str: searchVal
				}
			,	scope: this			
		});
	}

	,	renderMatchType: function( value, a ) {

			switch( value ) {
				case 'exact':
					return("Exact");
					
				case 'phonetic':
					return("Phonetic");

				case 'near_1':
					return("Near Match");

				case 'near_2':
					return("Possible Match");

				default:
					return("Unknown");
					break;
			}
		}

});